﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace LunarSF.SHomeWorkshop.LunarMarkdownEditor.Utils
{
    public class ResourceLink
    {
        private ResourceLink(string resourceLink)
        {
            this.link = resourceLink;

            var matchHeader = regHeader.Match(resourceLink);
            if (matchHeader.Success)
            {
                this.header = matchHeader.Value;
            }
            else this.header = "";

            var matchFilePath = regFilePath.Match(resourceLink);
            if (matchFilePath.Success)
            {
                this.filePath = matchFilePath.Value;

                var path = this.filePath.Replace("\\", "/");
                var lastIndexOfSlash = this.filePath.LastIndexOf("/");
                if (lastIndexOfSlash != -1)
                {
                    this.fileName = path.Substring(lastIndexOfSlash + 1);
                    var lastIndexOfDot = this.fileName.LastIndexOf(".");
                    if (lastIndexOfDot >= 0)
                    {
                        this.fileExtention = this.fileName.Substring(lastIndexOfDot + 1);
                        this.shortFileName = this.fileName.Substring(0, lastIndexOfDot);
                        this.type = GetType(this.fileExtention);
                    }
                    else
                    {
                        this.fileExtention = "";
                        this.shortFileName = this.fileName;
                    }
                }
                else
                {
                    this.fileName = "";
                    this.shortFileName = "";
                    this.fileExtention = "";
                }

            }
            else
            {
                this.filePath = "";
                this.fileExtention = "";
                this.shortFileName = "";
                this.fileName = "";
            }

            var regAppendInfo = new Regex(@"(?<=([ 　\t]{1,}"")).*?(?=""\)$)");
            var matchAppendInfo = regAppendInfo.Match(resourceLink);
            if (matchAppendInfo.Success)
            {
                this.appendInfo = matchAppendInfo.Value;
            }
            else { this.appendInfo = ""; }
        }

        public static ResourceLink BuildResourceLink(string resourceLink)
        {
            var regResLink = new Regex(@"!?\[.*?\]\(.*?\)");
            if (regResLink.Match(resourceLink).Success)
            {
                return new ResourceLink(resourceLink);
            }
            else
            {
                return null;
            }
        }

        private string header;

        public string Header { get { return this.header; } }

        private static Regex regHeader = new Regex(@"(?<=\[).*(?=\])");

        private string link;

        public string Link { get { return this.link; } }

        private string shortFileName;

        public string ShortFileName { get { return this.shortFileName; } }

        private string fileName;

        public string FileName { get { return this.fileName; } }

        private string fileExtention;

        public string FileExtention { get { return this.fileExtention?.ToLower(); } }

        private string filePath;

        public string FilePath { get { return this.filePath; } }

        private static Regex regFilePath = new Regex(@"(?<=\]\().*(?=([ 　\t]*).*\))");

        private string appendInfo;

        public string AppendInfo { get { return this.appendInfo; } }

        private ResourceLinkType type = ResourceLinkType.UnKnown;

        public ResourceLinkType Type { get { return this.type; } }

        public static ResourceLinkType GetType(string fileExtention)
        {
            switch (fileExtention.ToLower())
            {
                case "bmp":
                case "ico":
                case "png":
                case "gif":
                case "jpg":
                case "jpeg":
                case "tiff":
                    {
                        return ResourceLinkType.Image;
                    }
                case "mp3":
                case "wav":
                case "wma":
                    {
                        return ResourceLinkType.Sound;
                    }
                case "mp4":
                    {
                        return ResourceLinkType.Vedio;
                    }
                case "html":
                    {
                        return ResourceLinkType.Html;
                    }
                case "txt":
                    {
                        return ResourceLinkType.Text;
                    }
                default:
                    {
                        return ResourceLinkType.UnKnown;
                    }
            }
        }
    }

    public enum ResourceLinkType
    {
        Html,
        Text,
        Image,
        Sound,
        Vedio,
        UnKnown,
    }
}
